﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;

namespace QuickHelper
{
    public class Validators
    {
        /// <summary>
        /// Simple RegEx tester to ensure that the expression is a GUID
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsGUID(string expression)
        {
            if (!string.IsNullOrEmpty(expression))
            {
                Regex guidRegEx = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");

                return guidRegEx.IsMatch(expression);
            }
            return false;
        }

        /// <summary>
        /// Matches 98121 | 00988 | 12345-6789
        /// </summary>
        /// <param name="zipcode"></param>
        /// <returns></returns>
        public static bool IsValidZipCode(string zipcode)
        {
            if (!string.IsNullOrEmpty(zipcode))
            {
                Regex zipRegex = new Regex(@"^\d{5}(-\d{4})?$");

                return zipRegex.IsMatch(zipcode);
            }
            return false;
        }

        /// <summary>
        /// Matches 5305551212 | (530) 555-1212 | 530-555-1212
        /// </summary>
        /// <param name="phonenumber"></param>
        /// <returns></returns>
        public static bool IsValidPhoneNumber(string phonenumber)
        {
            if (!string.IsNullOrEmpty(phonenumber))
            {
                Regex phoneRegex = new Regex(@"^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$");
                return phoneRegex.IsMatch(phonenumber);
            }
            return false;
        }

        /// <summary>
        /// Matches abc@test.com | abc@test-test.info | abc.d-e_f+g@a.b-c.d--e.museum
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsValidEmail(string email)
        {
            if (!string.IsNullOrEmpty(email))
            {
                Regex emailRegex = new Regex(@"^([a-zA-Z0-9]+([\.+_-][a-zA-Z0-9]+)*)@(([a-zA-Z0-9]+((\.|[-]{1,2})[a-zA-Z0-9]+)*)\.[a-zA-Z]{2,6})$");
                return emailRegex.IsMatch(email);
            }
            return false;
        }
    }
}